package com.jsyl.lwbk.web.controller.api;

import com.jsyl.lwbk.business.domain.education.ChildReport;
import com.jsyl.lwbk.business.dto.ChildCourseScoreDTO;
import com.jsyl.lwbk.business.dto.ChildMonthPlanDTO;
import com.jsyl.lwbk.business.dto.ChildMonthResultDTO;
import com.jsyl.lwbk.business.dto.ChildScheduleReportDTO;
import com.jsyl.lwbk.business.mapper.education.ChildMonthPlanMapper;
import com.jsyl.lwbk.business.service.ChildMonthPlanService;
import com.jsyl.lwbk.business.service.IEvaluateReportService;
import com.jsyl.lwbk.business.service.education.ITrainingProgramChildRecordService;
import com.jsyl.lwbk.common.core.controller.BaseController;
import com.jsyl.lwbk.common.core.domain.AjaxResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Fitch
 * 2021/9/23
 */
@Api(value = "学生报告")
@Controller
@RequestMapping("/api/childReport")
public class ChildReportApiController extends BaseController {
    @Autowired
    private ChildMonthPlanService childMonthPlanService;

    @Autowired
    private ITrainingProgramChildRecordService iTrainingProgramChildRecordService;
    @Autowired
    private IEvaluateReportService iEvaluateReportService;
    @Autowired
    private ChildMonthPlanMapper childMonthPlanMapper;

    /**
     *
     * @param childId
     * @param reportType
     * @param projectType
     * @return
     */
    @ApiOperation(value = "按类型获取学生报告列表（废弃）")
    @ApiImplicitParams({@ApiImplicitParam(name = "childId", value = "学生ID"),
            @ApiImplicitParam(name = "reportType", value = "1-月度计划，2-月训练小结，3-课程报告，4-测评报告"),
            @ApiImplicitParam(name = "projectType", value = "1-感知觉与运动课，2-认知课，3-言语语言课（非必填）")})
    @GetMapping("/listByType")
    @ResponseBody
    public AjaxResult listByType(@RequestParam("childId") Long childId,
                                      @RequestParam(name = "reportType") Integer reportType,
                                      @RequestParam(name = "projectType",required = false) Integer projectType) {

        return success(this.childMonthPlanService.listByType(childId,reportType,projectType));

    }

    /**
     * 获取学生报告列表
     *
     * @param childId
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "获取学生报告列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "childId", value = "学生ID"),
            @ApiImplicitParam(name = "currentPage", value = "当前页：默认1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数")})
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult listChildReport(@RequestParam("childId") Long childId,
                                      @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        return success(this.childMonthPlanService.listChildReport(childId, currentPage, pageSize));

    }


    /**
     * @param id
     * @param reportType:1-月度计划，2-月训练小结，3-课程报告，4-测评报告
     * @return
     */
    @ApiOperation(value = "获取学生报告")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "列表返回的ID"),
            @ApiImplicitParam(name = "reportType", value = "列表返回的reportType")})
    @GetMapping("/getDetail")
    @ResponseBody
    public AjaxResult getChildMonthPlan(@RequestParam("id") Long id,
                                        @RequestParam("reportType") Integer reportType) {
        if (reportType != null && (reportType.equals(1)||reportType.equals(5))) {//1-月度计划
            ChildMonthPlanDTO childMonthPlanDTO = this.childMonthPlanService.getChildMonthPlan(id);
            if (childMonthPlanDTO == null) {
                return success();
            }
            this.childMonthPlanMapper.updateRead(id);
            return success(childMonthPlanDTO);
        }
        if (reportType != null && reportType.equals(2)) {//2-月训练小结
            ChildMonthResultDTO childMonthResultDTO = this.iTrainingProgramChildRecordService.getChildCourseMonthScoreReport(id);
            if (childMonthResultDTO == null) {
                return success();
            }
            this.childMonthPlanMapper.updateRead(id);
            return success(childMonthResultDTO);
        }
        if (reportType != null && reportType.equals(3)) {//3-课程报告
            ChildCourseScoreDTO childCourseScoreDTO = this.iTrainingProgramChildRecordService.getChildCourseScoreReport(id);
            if (childCourseScoreDTO == null) {
                return success();
            }
            this.childMonthPlanMapper.updateRead(id);
            return success(childCourseScoreDTO);
        }
        if (reportType != null && reportType.equals(4)) {//4-测评报告
            Map map = this.iEvaluateReportService.getChildReportMap(id);
            if (map == null) {
                return success();
            }
            this.childMonthPlanMapper.updateRead(id);
            return success(map);
        }

        return success();
    }


    @ApiOperation(value = "获取学生最新未读测评报告")
    @ApiImplicitParams({@ApiImplicitParam(name = "childId", value = "小孩ID"),
                        @ApiImplicitParam(name = "type", value = "请求类型：1-查看是否有测评，2-获取测评详情")})
    @GetMapping("/getChildEvaluateReport")
    @ResponseBody
    public AjaxResult getChildEvaluateReport(@RequestParam("childId") Long childId,
                                             @RequestParam("type") Integer type) {
        ChildReport childReport = childMonthPlanMapper.getChildEvaluateReport(childId);
        if (childReport != null) {
            if(type.equals(1)) {
                return AjaxResult.success("操作成功", childReport.getTrainingProgramNumber().split("-")[0]+"年"+childReport.getTrainingProgramNumber().split("-")[1]+"月"+childReport.getTrainingProgramNumber().split("-")[2]+"日的测评报告已生成");
            }
            if(type.equals(2)) {
                Map map = this.iEvaluateReportService.getChildReportMap(childReport.getId());
                if (map == null) {
                    return success();
                }
                this.childMonthPlanMapper.updateRead(childReport.getId());
                return success(map);
            }
        }
        return success();
    }

    @ApiOperation(value = "获取多个学生测评信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "childIds", value = "小孩ID(多个时，逗号分隔)")})
    @GetMapping("/getChildListEvaluateReport")
    @ResponseBody
    public AjaxResult getChildEvaluateReport(@RequestParam("childIds") String childIds) {
        return success(this.iEvaluateReportService.getChildListReport(childIds));
    }

    @ApiOperation(value = "根据测评编号获取测评信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "evaluateNumber", value = "测评编号")})
    @GetMapping("/getByEvaluateNumber")
    @ResponseBody
    public AjaxResult getByEvaluateNumber(@RequestParam("evaluateNumber") String evaluateNumber) {
        Map map = this.iEvaluateReportService.getByEvaluateNumber(evaluateNumber);
        if (map == null) {
            return success();
        }
        return success(map);
    }

    @ApiOperation(value = "根据日期获取课程报告")
    @ApiImplicitParams({@ApiImplicitParam(name = "childId", value = "小孩ID"),
                        @ApiImplicitParam(name = "dayDate", value = "日期(yyyy-MM-dd)")})
    @PostMapping("/listScheduleReport")
    @ResponseBody
    public AjaxResult listScheduleReport(@RequestBody() ChildScheduleReportDTO dto) {
        List<Map> map = this.childMonthPlanService.getScheduleReportIds(dto);
        if (map == null) {
            return success();
        }
        return success(map);
    }


}
